//
//  DPad.m
//  FunnyRacing
//
//  Created by vy phan on 16/03/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DPad.h"
#import "Button.h"
#import "Game.h"
#import "GraphicConstant.h"
#import "CGPointExtension.h"
#import "Define.h"

@implementation DPad
@synthesize direction;
-(id)init
{
	self = [super init];
	if (self != nil) {
//		Button *dpadUp = [[Button alloc]initWithType:DPAD_UP];
//		dpadUp.isTouchEnabled = YES;
//		dpadUp.position = ccp(100, 130);
//		
//		Button *dpadDown = [[Button alloc]initWithType:DPAD_DOWN];
//		dpadDown.isTouchEnabled = YES;
//		dpadDown.position = ccp(dpadUp.position.x, dpadUp.position.y - 100);
//		
//		Button *dpadLeft = [[Button alloc]initWithType:DPAD_LEFT];
//		dpadLeft.isTouchEnabled = YES;
//		dpadLeft.position = ccp(dpadUp.position.x - 50, dpadUp.position.y - 50);
//		
//		Button *dpadRight = [[Button alloc]initWithType:DPAD_RIGHT];
//		dpadRight.isTouchEnabled = YES;
//		dpadRight.position = ccp(dpadUp.position.x + 50, dpadUp.position.y - 50);
//		
//		[self addChild:dpadUp];
//		[self addChild:dpadDown];
//		[self addChild:dpadLeft];
//		[self addChild:dpadRight];
		spriteSheet = [CCSpriteSheet spriteSheetWithFile:@"arrows.png" capacity:1];
		dpadDown = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(DPAD_DOWN_POS * DPAD_BUTTON_W, 0, DPAD_BUTTON_W, DPAD_BUTTON_H)] 
													   selectedSprite:[CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(DPAD_DOWN_POS * DPAD_BUTTON_W, DPAD_BUTTON_H, DPAD_BUTTON_W, DPAD_BUTTON_H)]
#ifdef USE_KEY_PRESS										   
															target:self 
															 selector:@selector(keyDownPressed:)
#endif
					];
		
		dpadUp = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(DPAD_UP_POS * DPAD_BUTTON_W, 0, DPAD_BUTTON_W, DPAD_BUTTON_H)] 
													   selectedSprite:[CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(DPAD_UP_POS * DPAD_BUTTON_W, DPAD_BUTTON_H, DPAD_BUTTON_W, DPAD_BUTTON_H)]
				#ifdef USE_KEY_PRESS											   
																target:self 
															 selector:@selector(keyUpPressed:)
#endif
				  ];
		
		dpadRight = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(DPAD_RIGHT_POS * DPAD_BUTTON_W, 0, DPAD_BUTTON_W, DPAD_BUTTON_H)] 
													   selectedSprite:[CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(DPAD_RIGHT_POS * DPAD_BUTTON_W, DPAD_BUTTON_H, DPAD_BUTTON_W, DPAD_BUTTON_H)]
					#ifdef USE_KEY_PRESS		   
													target:self 
															 selector:@selector(keyRightPressed:)
#endif
					 ];
		
		dpadLeft = [CCMenuItemSprite itemFromNormalSprite:[CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(DPAD_LEFT_POS * DPAD_BUTTON_W, 0, DPAD_BUTTON_W, DPAD_BUTTON_H)] 
													   selectedSprite:[CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(DPAD_LEFT_POS * DPAD_BUTTON_W, DPAD_BUTTON_H, DPAD_BUTTON_W, DPAD_BUTTON_H)]
													#ifdef USE_KEY_PRESS		   
												   target:self 
															 selector:@selector(keyLeftPressed:)
#endif
					];
		
		dpadUp.position = ccp(100, 130);
		dpadDown.position = ccp(dpadUp.position.x, dpadUp.position.y - 100);
		dpadLeft.position = ccp(dpadUp.position.x - 50, dpadUp.position.y - 50);
		dpadRight.position = ccp(dpadUp.position.x + 50, dpadUp.position.y - 50);


		
		menu = [CCMenu menuWithItems:dpadUp, dpadDown, dpadLeft, dpadRight, nil];
		menu.position = CGPointZero;
		[self addChild:menu];
#ifndef USE_KEY_PRESS
		[self schedule:@selector(checkKey:) interval:1.0/60];
#endif
	}
	direction = -1;
	return self;
}
#ifndef USE_KEY_PRESS
- (void)checkKey:(ccTime)dt
{
	if (dpadUp.isSelected) {
		direction = DPAD_UP;
	}
	else
	if (dpadDown.isSelected) {
		direction = DPAD_DOWN;
	}
	else
	if (dpadLeft.isSelected) {
		direction = DPAD_LEFT;
	}
	else
	if (dpadRight.isSelected) {
		direction = DPAD_RIGHT;
	}
	else {
		direction = -1;
	}

	

}
#endif
#ifdef USE_KEY_PRESS
- (void)keyDownPressed:(id)sender
{
	direction = DPAD_DOWN;
	printf("direction = DPAD_DOWN %d\n", direction);
}
- (void)keyUpPressed:(id)sender
{
	direction = DPAD_UP;
	printf("direction = DPAD_UP %d\n", direction);
}
- (void)keyRightPressed:(id)sender
{
	direction = DPAD_RIGHT;
	printf("direction = DPAD_RIGHT %d\n", direction);
}
- (void)keyLeftPressed:(id)sender
{
	direction = DPAD_LEFT;
	printf("direction = DPAD_LEFT %d\n", direction);
}
-(void)keyReleased:(id)sender
{
	direction = -1;
	printf("direction = %d", direction);
}
#endif
@end
